package cn.com.bluemoon.daps.system.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.dto.MenuDto;
import cn.com.bluemoon.daps.system.dto.SysPageDto;
import cn.com.bluemoon.daps.system.entity.DapSysMenu;
import cn.com.bluemoon.daps.system.service.DapSysMenuService;
import cn.com.bluemoon.daps.system.vo.DapSysMenuTreeVo;
import cn.com.bluemoon.daps.system.vo.DapSysMenuVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-12-19
 */
@Api(tags = {"系统菜单管理"})
@RestController
@Slf4j
@RequestMapping("/dapSys")
public class DapSysMenuController {

    @Autowired
    private DapSysMenuService sysMenuService;

    @ApiOperation("菜单列表")
    @PostMapping(value = "/menu/page")
    @BmPermission(value = "dap:system:menu:page",ignore = true)
    public ResultBean<IPage<DapSysMenuVo>> page(@RequestBody SysPageDto pageDto){
        IPage<DapSysMenuVo> menuPage = sysMenuService.menuPage(pageDto);
        return ResultBean.ok(menuPage);
    }

    @ApiOperation("根据ID获取菜单")
    @GetMapping(value = "/menu/getMenuById")
    @BmPermission(value = "dap:system:menu:getMenuById",ignore = true)
    public ResultBean<DapSysMenu> getMenuById(@RequestParam("id") Long id){
        DapSysMenu sysMenu = sysMenuService.getById(id);
        if(Objects.isNull(sysMenu)){
            return ResultBean.error(String.format("该菜单已不存在"));
        }
        return ResultBean.ok(sysMenu);
    }

    @ApiOperation("获取菜单树")
    @GetMapping(value = "/menu/getMenuTree")
    @BmPermission(value = "dap:system:menu:getMenuTree",ignore = true)
    public ResultBean<List<DapSysMenuTreeVo>> getMenuTree(){
        return ResultBean.ok(sysMenuService.getMenuTree(0L));
    }

    @ApiOperation("新增菜单")
    @PostMapping(value = "/menu/add")
    @BmPermission(value = "dap:system:menu:add",ignore = true)
    public ResultBean<Boolean> add(@RequestBody MenuDto dto){
        if(Objects.nonNull(dto.getId())){
            dto.setId(null);
        }
        return ResultBean.ok(sysMenuService.saveMenu(dto));
    }

    @ApiOperation("修改菜单")
    @PostMapping(value = "/menu/update")
    @BmPermission(value = "dap:system:menu:update",ignore = true)
    public ResultBean<Boolean> update(@RequestBody MenuDto dto){
        if(Objects.isNull(dto.getId())){
            return ResultBean.error(String.format("菜单 %s 的ID不能为空", dto.getName()));
        }
        DapSysMenu sysMenu = sysMenuService.getById(dto.getId());
        if(Objects.isNull(sysMenu)){
            return ResultBean.error(String.format("菜单 %s 已不存在", dto.getName()));
        }
        return ResultBean.ok(sysMenuService.saveMenu(dto));
    }

    @ApiOperation("删除菜单")
    @DeleteMapping(value = "/menu/delete")
    @BmPermission(value = "dap:system:menu:delete",ignore = true)
    public ResultBean<Boolean> delete(@RequestParam("id") Long id){
        return ResultBean.ok(sysMenuService.removeById(id));
    }


}
